//
//  SXNewsRootViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 张琪. All rights reserved.
//

#define kScale 0.6

#import "SXNewsRootViewController.h"
#import "SXBaseRefreshViewController.h"
#import "UIView+UIView.h"
#import "AppDelegate.h"

@interface SXNewsRootViewController () <UIScrollViewDelegate>

@property(nonatomic, strong)UIScrollView *headerScrollView;
@property(nonatomic, strong)UIScrollView *mainScrollView;

@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, strong) SXBaseRefreshViewController *beforeMainVC;
@property (nonatomic, strong) SXBaseRefreshViewController *currentMainVC;
@property (nonatomic, strong) SXBaseRefreshViewController *nextMainVC;

@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray *listArray;

@end

@implementation SXNewsRootViewController

- (UIScrollView *)headerScrollView{
    if (!_headerScrollView) {
        _headerScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 30)];
        _headerScrollView.contentSize = CGSizeMake(50 * self.titleArray.count, 0);
        [_headerScrollView setShowsHorizontalScrollIndicator:NO];
        _headerScrollView.backgroundColor = [UIColor whiteColor];
        for (int i = 0; i < self.titleArray.count; i++) {
            UIButton *titleButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
            titleButton.frame = CGRectMake(50 * i, 0, 50, 30);
            titleButton.tag = 100 + i;
            titleButton.titleLabel.font = [UIFont systemFontOfSize:15];
            [titleButton setTitle:self.titleArray[i] forState:(UIControlStateNormal)];
            [titleButton addTarget:self action:@selector(changeView:) forControlEvents:(UIControlEventTouchUpInside)];
            if (i == 0) {
                [titleButton setTintColor:[UIColor redColor]];
                titleButton.titleLabel.font = [UIFont systemFontOfSize:16];
            }
            else
            {
                [titleButton setTintColor:[UIColor lightGrayColor]];
            }
            [_headerScrollView addSubview:titleButton];
        }
    }
    return _headerScrollView;
}

- (UIScrollView *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 94, kScreenWidth, kScreenHeight - 143)];
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.bounces = NO;
        _mainScrollView.contentSize = CGSizeMake(kScreenWidth * self.listArray.count, 0);
        _mainScrollView.delegate = self;
        _mainScrollView.backgroundColor = [UIColor clearColor];
        
        _currentMainVC = [[NSClassFromString(self.listArray[0][1]) alloc] init];
        _currentMainVC.rootRightVC = self.rootRightVC;
        _currentMainVC.URLString = self.listArray[0][0];
        _currentMainVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 143);
        [_mainScrollView addSubview:_currentMainVC.view];
        
        _nextMainVC = [[NSClassFromString(self.listArray[1][1]) alloc] init];
        _nextMainVC.rootRightVC = self.rootRightVC;
        _nextMainVC.URLString = self.listArray[1][0];
        _nextMainVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight - 143);
        [_mainScrollView addSubview:_nextMainVC.view];
        
        // 左边缘轻扫手势
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:delegate.rootVC action:@selector(edgePan:)];
        edgePan.edges = UIRectEdgeLeft;
        [_mainScrollView addGestureRecognizer:edgePan];
    }
    return _mainScrollView;
}

// 点击上面的scrollView
- (void)changeView:(UIButton *)button
{
    NSInteger currentIndex = button.tag - 100;
    [self changeMainViewWithIndex:currentIndex];
    _mainScrollView.contentOffset = CGPointMake(kScreenWidth * currentIndex, 0);
}

- (void)changeMainViewWithIndex:(NSInteger)currentIndex
{
    NSInteger j = _currentIndex;
    NSInteger i = currentIndex;
    
    UIButton *beforeButton = (UIButton *)[self.view viewWithTag:100 + j];
    [beforeButton setTintColor:[UIColor lightGrayColor]];
    beforeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    UIButton *currentButton = (UIButton *)[self.view viewWithTag:100 + i];
    [currentButton setTintColor:[UIColor redColor]];
    currentButton.titleLabel.font = [UIFont systemFontOfSize:16];
    
    _currentIndex = i;
    
    [_mainScrollView removeAllSubviews];
    
    _currentMainVC = [[NSClassFromString(self.listArray[i][1]) alloc] init];
    _currentMainVC.rootRightVC = self.rootRightVC;
    _currentMainVC.URLString = self.listArray[i][0];
    _currentMainVC.view.frame = CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight - 143);
    [_mainScrollView addSubview:_currentMainVC.view];
    if (i != 0) {
        _beforeMainVC = [[NSClassFromString(self.listArray[i - 1][1]) alloc] init];
        _beforeMainVC.rootRightVC = self.rootRightVC;
        _beforeMainVC.URLString = self.listArray[i - 1][0];
        _beforeMainVC.view.frame = CGRectMake(kScreenWidth * (i - 1), 0, kScreenWidth, kScreenHeight - 143);
        [_mainScrollView addSubview:_beforeMainVC.view];
    }
    if (i != self.listArray.count - 1) {
        _nextMainVC = [[NSClassFromString(self.listArray[i + 1][1]) alloc] init];
        _nextMainVC.rootRightVC = self.rootRightVC;
        _nextMainVC.URLString = self.listArray[i + 1][0];
        _nextMainVC.view.frame = CGRectMake(kScreenWidth * (i + 1), 0, kScreenWidth, kScreenHeight - 143);
        [_mainScrollView addSubview:_nextMainVC.view];
    }
}

// 下面的scrollView滑动过程中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > _currentIndex * kScreenWidth) {
        CGFloat currentScale = (scrollView.contentOffset.x - _currentIndex * kScreenWidth) / kScreenWidth;
        _currentMainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - currentScale * kScale, 1 - currentScale * kScale);
        _nextMainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - kScale + currentScale * kScale,  1 - kScale + currentScale * kScale);
    }
    else
    {
        CGFloat currentScale = (_currentIndex * kScreenWidth - scrollView.contentOffset.x) / kScreenWidth;
        _currentMainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - currentScale * kScale, 1 - currentScale * kScale);
        _beforeMainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - kScale + currentScale * kScale,  1 - kScale + currentScale * kScale);
        
    }
    NSInteger currentIndex = scrollView.contentOffset.x / kScreenWidth;
    if (currentIndex != _currentIndex ) {
        [UIView animateWithDuration:0.5 animations:^{
            NSInteger currentOffsetX = 50 * currentIndex - kScreenWidth / 2 + 25;
            currentOffsetX = currentOffsetX < 50 * self.titleArray.count - kScreenWidth ? currentOffsetX : 50 * self.titleArray.count - kScreenWidth;
            currentOffsetX = currentOffsetX > 0 ? currentOffsetX : 0;
            _headerScrollView.contentOffset = CGPointMake(currentOffsetX, 0);
        }];
        [self changeMainViewWithIndex:currentIndex];
    }
}

- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    [self readData];
    UIImageView *mainBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    mainBackImageView.image = [UIImage imageNamed:@"SX-Xinwen"];
    [self.view addSubview:mainBackImageView];
    [self.view addSubview:self.headerScrollView];
    [self.view addSubview:self.mainScrollView];
    
    _currentIndex = 0;
    // Do any additional setup after loading the view.
}

- (void)readData
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"SXNews" ofType:@"plist"];
    NSArray *allListArray = [NSArray arrayWithContentsOfFile:filePath];
    for (NSDictionary *dict in allListArray) {
        [self.titleArray addObject:[[dict allKeys] firstObject]];
        [self.listArray addObject:dict[[[dict allKeys] firstObject]]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
